<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户信息页面</title>
<link rel="stylesheet" href="layui/css/layui.css" media="all">

<style>
.layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
	height: 34px;
	line-height: 34px;
	padding: 0 8px;
}
</style>
</head>
<body>
	<div class="layuimini-container">
		<div class="layuimini-main">
			<div class="layui-form" style="margin-top: 20px;" onsubmit="false">
				<div class="layui-form-item">
					<label class="layui-form-label">用户姓名:</label>
					<div class="layui-input-inline">
						<input type="text" name="username" id="username"
							placeholder="请输入用户的姓名" class="layui-input">
					</div>
					<label class="layui-form-label">用户状态:</label>
					<div class="layui-input-inline" style="width: 120;">
							<select name="IsLockout" id="IsLockout" class="layui-input">
								<option value="">请筛选是否禁用</option>
								<option value="1">启用</option>
								<option value="2">禁用</option>
							</select>
						</div>
					<button class="layui-btn" id="search" data-type="reload">搜索</button>
				</div>
			</div>
			<div>
				<table class="layui-hide" id="test" lay-filter="test"></table>
			</div>
		</div>
	</div>


	<!-- 操作列 -->
	<script type="text/html" id="barDemo">
			<a class="layui-btn layui-btn-xs" lay-event="editusers">编辑</a>
            <a class="layui-btn layui-btn-xs" lay-event="setusersroles">设置角色</a>
            <a class="layui-btn layui-btn-xs layui-btn-normal layui-btn-radius" lay-event="resetpassword">重置密码</a>
			<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delusers">删除</a>
		</script>
	<script type="text/html" id="toolbarDemo">
      <!--表头上面引入了一个添加用户的操作-->
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="addusers">添加</button>
      </div>
    </script>
	<script src="layui/layui.js" charset="utf-8"></script>
	<script src="js/Global.js" type="text/javascript" charset="utf-8"></script>
	<script>
		layui.use([ 'form', 'table' ],  function() { //如果只加载一个模块，可以不填数组。如：layui.use('form')
			var table = layui.table; //获取table模块
			var form = layui.form;
			var $ = layui.jquery;
			// 渲染表格
			table.render({
				elem : '#test',
				url : globalData.server+'public/getAllUsers',
				toolbar : '#toolbarDemo', //开启头部工具栏
				where : {
					method : 'getAll',
					username : '',
					IsLockout:''
				},
				method : 'POST',
				cols : [ [ {
					field : 'userid',
					title : '用户ID',
					fixed : 'left',
					align:'center',
					sort : true,
					width : 120,
				}, {
					field : 'username',
					title : '用户姓名',
					align:'center',
					width : 110,
				}, {
					field : 'userpassword',
					title : '用户密码',
					align:'center',
					width : 110,
				}, {
					field : 'IsLockout',
					title : '是否禁用',
					templet: '#switchTpl',
					align:'center',
					width : 110,
				}, {
					field : 'LastLoginTime',
					title : '最后登录时间',
					templet : "<div>{{!d.LastLoginTime?'':layui.util.toDateString(d.LastLoginTime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					field : 'CreateTime',
					title : '账户创立时间',
					templet : "<div>{{!d.CreateTime?'':layui.util.toDateString(d.CreateTime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					field : 'PsdWrongCount',
					title : '密码错误次数',
					align:'center',
					width : 125,
				}, {
					field : 'LockTime',
					title : '被锁定时间',
					templet : "<div>{{!d.LockTime?'':layui.util.toDateString(d.LockTime, 'yyyy-MM-dd HH:mm:ss')}}</div>", 
					align:'center',
					width : 180,
				}, {
					field : 'ProtectEmail',
					title : '密保邮箱',
					align:'center',
					width : 150,
				}, {
					field : 'ProtectTel',
					title : '密保手机号',
					align:'center',
					width : 120,
				}, {
					title : '操作',
					fixed : 'right',
					align : 'center',
					toolbar : '#barDemo',
					width : 300
				} ] ],
				id : 'testReload',
				page : true
			});
			
			
			
			form.on('switch(u_stateDemo)', function(data) {
					              var IsLockout = this.checked ? 1 : 2;
					              var userid = this.value;
					              $.ajax({
					            	  url :globalData.server+'public/updateuserstate',
					                type : "get",
					                data : {
					                	IsLockout : IsLockout,
					                	userid : userid
					                },
					                success : function(d) {
					                  if (d.code == 1) {
					                    layer.msg("操作成功！");
					                  }else if (d.code == 2) {
					                    layer.msg("操作成功！");
					                  }else {
					                    layer.msg("操作失败！", {
					                      icon : 5
					                    });
					                  }
					                }
					              })
					            });
			
			
			
			$('#search').on('click', function() {
				var username = $("#username").val();
				var IsLockout = $("#IsLockout").val();
				table.reload('testReload', {
					where : {
						'username' : username,
						'IsLockout':IsLockout
					}
				});
			});
			//监听提交按钮
			table.on('tool(test)', function(obj) {
				var data = obj.data;
				if (obj.event === 'delusers') {
					layer.confirm('真的删除该用户信息吗?', function(index) {
						console.log(obj.data);
						var url = globalData.server+'public/deleteUsers';
						$.post(url, {
							"userid" : obj.data.userid,
							"uid":globalData.getUid(),
							method : 'delete'
						}, function(res) {
							if(res.count>0) {
								layer.msg("删除成功", {
									icon: 1,
									time: 3000
								});
								window.location.reload();
								obj.del();
							} else if(res.count == -1){
								layer.msg("删除失败,该用户下分配有角色！！！", {
									icon: 2,
									time: 3000
								})
							}else if(res.count == -2){
								layer.msg("删除失败,用户不能删除自己", {
									icon: 2,
									time: 3000
								})
							}
						}, "json");
						layer.close(index);
					});
				} else if (obj.event === 'editusers') {
					console.log(obj.data);
					layer.open({ //打开一个页面
						type : 2, //在当前的页面之上，弹框形式，展示出添加用户的界面
						area : [ '700px', '500px' ], //页面的宽高
						content : 'users_edit.html', //页面所在的位置
						title : '编辑用户信息',
						fix:false,
						maxmin:true,
						success : function(layero, index) {
							console.log(obj.data);
							var body = layer.getChildFrame('body', index);
							body.contents().find("#userid").val(obj.data.userid);
							body.contents().find("#username").val(obj.data.username);
							body.contents().find("#userpassword").val(obj.data.userpassword);
							body.contents().find("#IsLockout2").val(obj.data.IsLockout);
							body.contents().find("#LastLoginTime").val(layui.util.toDateString(obj.data.LastLoginTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#CreateTime").val(layui.util.toDateString(obj.data.CreateTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#PsdWrongCount").val(obj.data.PsdWrongCount);
							body.contents().find("#LockTime").val(layui.util.toDateString(obj.data.LockTime, 'yyyy-MM-dd HH:mm:ss'));
							body.contents().find("#ProtectEmail").val(obj.data.ProtectEmail);
							body.contents().find("#ProtectTel").val(obj.data.ProtectTel);
						},
						end : function() {
							window.location.reload();
						}
					});
				}else if (obj.event === 'resetpassword') {
				layer.confirm('您确定要重置 '+data.username+' 的密码为默认密码 123456 吗?', function(index) {
					$.post(globalData.server + "public/resetPassword", {
						"userid" : data.userid
					}, function(res) {
						if(res.count>0) {
							layer.msg("重置成功", {
								icon: 1,
								time: 2000
							});
						
						} else {
							layer.msg("重置失败", {
								icon: 2,
								time: 3000
							})
						}
						
						
					}, "json");
					    var index = parent.layer.getFrameIndex(window.name); //获取当前窗口的name
						parent.layer.close(index); //关闭窗口
				        window.location.reload();
				});
				}else if (obj.event === 'setusersroles') {
            		var index = layer.open({
					title : '给用户'+ data.username +'设置角色',
					type : 2,
					shade : 0.2,
					maxmin : true,
					shadeClose : true,
					area : [ '550px', '510px' ], //页面的宽高
					content : 'SetUsersroles.html',
					success : function(layero, index) {
						var body = layer.getChildFrame('body', index);
							body.find("#userid").val(data.userid);
					},
					end : function() {
						window.location.reload();
					}
				});
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
				
				
				
				
			});
			table.on('toolbar(test)', function(obj) {
				switch (obj.event) {
				case 'addusers':
					layer.open({
						type : 2,
						area : [ '600px', '500px' ], //页面的宽高
						content : 'users_add.html',
						title : '添加用户信息',
						fix:false,
						maxmin:true,
						end : function() {
							window.location.reload();
						}
					});
					break;
				}
				;
			});
		});
		layui.use('laydate', function() {
			var laydate = layui.laydate;
			//同时绑定多个时间选择器
			laydate.render({
				elem : '#begintime',
				type : 'datetime'
			});
			laydate.render({
				elem : '#endtime',
				type : 'datetime'
			});
		});
	</script>
	<script type="text/html" id="switchTpl">
  <input type="checkbox" name="IsLockout" value="{{d.userid}}" lay-skin="switch"
 lay-text="启用|禁用" lay-filter="u_stateDemo" {{ d.IsLockout == 1 ? 'checked' : '' }}>
</script>
</body>
</html>